package com.game.doudizhu.repository;

import com.game.doudizhu.model.Card;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CardRepository extends JpaRepository<Card, Long> {
    
    // 根据房间ID查找卡牌
    List<Card> findByRoomId(Long roomId);
    
    // 根据玩家ID查找卡牌
    List<Card> findByPlayerId(Long playerId);
    
    // 根据房间ID删除卡牌
    @Modifying
    @Transactional
    @Query("DELETE FROM Card c WHERE c.roomId = :roomId")
    void removeCardsByRoomId(Long roomId);
    
    // 根据玩家ID删除卡牌
    @Modifying
    @Transactional
    @Query("DELETE FROM Card c WHERE c.playerId = :playerId")
    void deleteByPlayerId(Long playerId);
    
    // 根据房间ID和玩家ID查找卡牌
    List<Card> findByRoomIdAndPlayerId(Long roomId, Long playerId);
    
    // 根据房间ID和状态查找卡牌
    List<Card> findByRoomIdAndStatus(Long roomId, Card.CardStatus status);
    
    // 检查卡牌是否存在
    boolean existsByRoomIdAndId(Long roomId, Long cardId);
    
    // 批量更新卡牌状态
    @Modifying
    @Transactional
    @Query("UPDATE Card c SET c.status = :status WHERE c.id IN :cardIds")
    void updateCardStatus(List<Long> cardIds, Card.CardStatus status);
}